package com.xinzhi.service.impl;

import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.util.TypeKey;
import com.xinzhi.mapper.UserMapper;
import com.xinzhi.pojo.User;
import com.xinzhi.service.IUserService;
import com.xinzhi.utils.JwtUtil;
import com.xinzhi.utils.RedisUtils;
import com.xinzhi.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Resource
    private RedisUtils redisUtils;
    @Resource
    private ObjectMapper objectMapper;
    private User topUser = null;

   private String key = null;
    @Override
    public String createJwt(User user) {
        String token = JwtUtil.createJWT(user.getPhone(), user.toString(), 6 * 60 * 1000l);
        redisUtils.set(user.getPhone(), token,6*60);
        key = user.getPhone();
        return token;
    }


    @Override
    public Result login(User user) {
        if (user.getPhone() != null && user.getPwd() != null) {
            if (!user.getPhone().matches("\\d{11}") || !user.getPwd().matches("\\w{8,12}"))
                return Result.error(400, "手机号格式或密码错误");
            user.setPwd(DigestUtil.md5Hex(user.getPwd()));
            QueryWrapper<User> wrapper = new QueryWrapper<User>().and(i -> i.eq("phone", user.getPhone()).eq("pwd", user.getPwd()));
            topUser = userMapper.selectOne(wrapper);
            System.out.println(topUser);
            String token = createJwt(user);
            return topUser != null ? Result.ok("令牌是"+token+"-----》登录的用户是\n"+topUser.toString()) : Result.error(400, "电话或密码错误,存入失败");
        }
        return Result.error(400, "用户名或密码错误");
    }
    @Override
    public Result register(User user) {
        if (userMapper.findByPhone(user.getPhone()) != null) {
            return Result.error(400, "手机号已存在");
        }
        if (user.getPhone() != null && user.getPwd() != null) {
            if (user.getPhone().matches("\\d{11}") && user.getPwd().matches("\\w{8,12}")) {
                user.setPwd(DigestUtil.md5Hex(user.getPwd()));
                int count = userMapper.insert(user);
                return count > 0 ? Result.ok("注册成功") : Result.error(400, "注册失败");
            }
        }
        return Result.error(400, "您的输入有错误导致注册失败");
    }
    @Override
    public Result updateUser(User user) {
//        if (token == null) {
//            return Result.error(400, "token为空,修改失败");
//        }
//        if (token.equals(redisUtils.get(key)) ){
//            if (user == null) {
//                return Result.error(400, "传过来的用户为空,修改失败");
//            }
            user.setId(topUser.getId());
            if (topUser != null && user.getPhone() != null) {
                if (!user.getPhone().matches("\\d{11}"))
                    return Result.error(400, "手机号为空,修改失败");
            }
            if (user.getPwd() != null) {
                if (!user.getPwd().matches("\\w{8,12}"))
                    return Result.error(400, "密码修改失败");
            }
            if (user.getPid() != null) {
                if (!user.getPid().matches("[1-9]\\w{17}"))
                    return Result.error(400, "身份证号修改失败");
            }

            int count = userMapper.updateUser(user);
            // boolean set = redisUtils.set(user.getPhone(), user);
            return count > 0 ? Result.ok("修改用户信息成功") : Result.error(400, "修改用户信息失败");
        }
//        return Result.error(400, "token错误,修改失败");
//    }
        @Override
        public Result deleteUser (){
            boolean del = false;
            //延时双删
            if (topUser == null){
                return Result.error(400, "您未登录，删除失败");
            }
          //  del = redisUtils.del(topUser.getPhone());
            int count = userMapper.deleteUser(topUser);
            try {
                Thread.sleep(5000);
             //   del = redisUtils.del(topUser.getPhone());
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return count > 0 ? Result.ok("删除用户成功") : Result.error(400, "删除用户失败");
        }
        @Override
        public String getUser() {
            if (topUser == null) {
                return "您未登录";
            }
//        topUser.setPwd(DigestUtil.md5Hex(topUser.getPwd()));
//        topUser.setPwd("******");
          //  return redisUtils.get(topUser.getPhone()).toString();
            return   topUser.toString();

        }
    @Override
    public Result secondLogin(String token) {
        if(token!=null&&token .equals(redisUtils.get(key)) ){
            return Result.ok("令牌验证成功,可以去修改用户信息了");
        }
        return Result.error(400,"令牌验证失败,请重新登录");
    }
}


